﻿using System;
using System.Collections.Generic;
using System.Linq;
using Macad.Core.Topology;
using Macad.Common.Serialization;
using Macad.Occt;
using Macad.Common;

namespace Macad.Core.Shapes
{
    public class EdgeSelectionShape : Shape
    {
        private readonly Dictionary<TopoDS_Edge, bool> _EdgeSelectionStatus = new();

        public List<TopoDS_Edge> SelectedEdges { get; } = new();

        public EdgeSelectionShape()
        {
            // Initialization if needed
        }

        public override ShapeType ShapeType => ShapeType.Solid;

        public void ToggleEdgeSelection(TopoDS_Edge edge)
        {
            if (_EdgeSelectionStatus.ContainsKey(edge))
            {
                if (_EdgeSelectionStatus[edge])
                {
                    RemoveEdge(edge);
                }
                else
                {
                    AddEdge(edge);
                }
            }
            else
            {
                AddEdge(edge);
            }
        }

        public void AddEdge(TopoDS_Edge edge)
        {
            if (!_EdgeSelectionStatus.ContainsKey(edge))
            {
                _EdgeSelectionStatus[edge] = true;
                SelectedEdges.Add(edge);
                HighlightEdge(edge);
            }
        }

        public void RemoveEdge(TopoDS_Edge edge)
        {
            if (_EdgeSelectionStatus.ContainsKey(edge) && _EdgeSelectionStatus[edge])
            {
                _EdgeSelectionStatus[edge] = false;
                SelectedEdges.Remove(edge);
                UnhighlightEdge(edge);
            }
        }

        private void HighlightEdge(TopoDS_Edge edge)
        {
            // Implement the method to highlight the edge
        }

        private void UnhighlightEdge(TopoDS_Edge edge)
        {
            // Implement the method to unhighlight the edge
        }

        public void ClearSelection()
        {
            foreach (var edge in SelectedEdges.ToList())
            {
                RemoveEdge(edge);
            }
        }

        protected override bool MakeInternal(MakeFlags flags)
        {
            // Implement shape creation logic here
            // For example, you might create a simple box shape
            BRep = CreateBox();
            return BRep != null;
        }

        private TopoDS_Shape CreateBox()
        {
            var boxMaker = new BRepPrimAPI_MakeBox(100, 100, 100);
            return boxMaker.Shape();
        }
    }
}
